/**
 * 
 */
package reco.db.validation;

import static com.google.common.base.Preconditions.checkArgument;
import static org.apache.commons.lang.StringUtils.isBlank;
import reco.db.Constraint;

/**
 * Restriccion empleada para asegurarse que un campo de string no quede en blanco.
 */
public final class MinSizeConstraint extends Constraint<String> {

    private final int minSize;

    public MinSizeConstraint(int constraintMinSize) {
        super("minSize", "tooShort");
        checkArgument(constraintMinSize > 0, "constraintMinSize must be greater than 0");
        this.minSize = constraintMinSize;
    }

    @Override
    public boolean validate(String value) {
        if (isBlank(value)) {
            return true;
        }
        return value.length() >= this.minSize;
    }

    @Override
    public Object[] getErrorArguments() {
        return new Object[] { this.minSize };
    }
}
